<%@page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/taglib/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/taglib/struts-html.tld" prefix="html"%>

<!-- ############################ Validation of message ############################  -->
<script type="text/javascript">
	function isValidUpdate() {
		var isValid = true;
		if (document.getElementById("discipline").firstChild.value == "") {
			alert("Выберите учебную дисциплину!");
			isValid = false;
		}
		return isValid;
	}
	
	function updateForm(){
		var text = $('textarea[name="message"]').val();
		var file = $('input[name="fileAttach"]').val();
		if(isValidUpdate()){
			$.ajax({
				type : 'POST',
				url : '/SCS_BSU/message.do',
				data : 'courseId='+$("select[name='courseId'] option:selected").val()
				+'&disciplineId='+$("select[name='disciplineId'] option:selected").val()+
				'&groupId='+$("select[name='groupId'] option:selected").val()+'&method=update',
				success : function(data) {
					$(".ajax_result").html(data);
					$('textarea[name="message"]').val(text);
					$('textarea[name="fileAttach"]').val(file);
				},
				error : function(error) {
					alert("В результате выполнения ajax-запроса произошла ошибка");
					$('textarea[name="message"]').val(text);
					$('textarea[name="fileAttach"]').val(file);
				}
			});
		}
	}

	$(document).ready(function() {
		$("select[name='courseId'] option, select[name='disciplineId'] option, select[name='groupId'] option").click(function(){
			updateForm();
		});
	});
</script>

<div id="content" class="float_left">
<h3><bean:message key="common.message_create.messages.new_message" />
</h3>

<html:form action="/message">
	<table class="custom_table">
		<tr>
			<td><bean:message
				key="instructor.message_create.messages.choose_discipline" /><span
				style="color: red;">*</span> :<br />
			<div style="color: red; font-size: 11px;"><html:errors
				property="discipline" /></div>
			</td>
			<td id="discipline"><html:select name="messageForm"
				property="disciplineId">
				<html:option value=""></html:option>
				<html:optionsCollection name="messageForm" property="disciplineList"
					label="label" value="value" />
			</html:select></td>
		</tr>
		<tr>
			<td><bean:message
				key="instructor.message_create.messages.course" /> :</td>
			<td><html:select name="messageForm" property="courseId">
				<html:option value=""></html:option>
				<html:optionsCollection name="messageForm" property="courseList"
					label="label" value="value" />
			</html:select></td>
		</tr>
		<tr>
			<td><bean:message key="instructor.message_create.messages.group" />
			:</td>
			<td><html:select name="messageForm" property="groupId">
				<html:option value=""></html:option>
				<html:optionsCollection name="messageForm" property="groupList"
					label="label" value="value" />
			</html:select></td>
		</tr>
	
	</table>
</html:form>
<div class="ajax_result">
	<html:form action="/message/sendInstructor"
		enctype="multipart/form-data">
	
		<table class="custom_table">
	
			<tr>
				<td><bean:message key="common.message_create.messages.user_to" />
				:</td>
				<td><html:select name="messageForm" property="userToId">
					<html:option value="0">
						<bean:message key="common.messages.all" />
					</html:option>
					<html:optionsCollection name="messageForm" property="userList"
						label="label" value="value" />
				</html:select></td>
			</tr>
			<tr>
				<td><bean:message key="common.messages.message" /><span
					style="color: red;">*</span> :<br />
				<div style="color: red; font-size: 11px;"><html:errors
					property="message" /></div>
				</td>
				<td><html:textarea cols="40" rows="8" name="messageForm"
					property="message" /></td>
			</tr>
			<tr>
				<td><bean:message key="common.messages.attach_file" /> :<br />
				<div style="color: red; font-size: 11px;"><html:errors
					property="fileAttach" /></div>
				</td>
				<td><html:file name="messageForm" property="fileAttach" /></td>
			</tr>
			<tr>
				<td></td>
				<td><html:submit property="method" value="send" /></td>
			</tr>
		</table>
	</html:form>
</div>
</div>